/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PanelIndex.java
 *
 * Created on Nov 3, 2010, 5:36:37 PM
 */
package GUI;

import DBHandling.DBUtil;
import DBHandling.ReturnRecieptHandling;
import java.awt.Color;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.RowFilter;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.view.JasperViewer;

/**
 *
 * @author Vangnd
 */
public class BillManagementPanel extends javax.swing.JPanel {

    //declare requred object
    DBUtil objDB = new DBUtil();
    String loginUser;
    Connection conn;
    DefaultTableModel itemModel = null;
    DefaultTableModel billModel = null;
    JTable tblReturned = null;
    DefaultTableModel returnedModel = null;
    ResultSet rsBill = null;
    ResultSet rsItem = null;
    ResultSet rsReciept = null;
    ReturnRecieptHandling rrh = null;
    HashMap<String, String> m = null;
    InputStream is = null;
    JasperPrint jp = null;
    private TableRowSorter<TableModel> sorter;

    /**
     * Creates new form PanelIndex
     */
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    public BillManagementPanel(String user) throws SQLException {
        initComponents();
        //initial object
        objDB = new DBUtil();
        conn = objDB.connect();
        loginUser = user;
        tblReturned = new JTable();
        rrh = new ReturnRecieptHandling();
        returnedModel = new DefaultTableModel();
        tblReturned.setModel(returnedModel);
        //add column title to return table (this table are invisible)
        returnedModel.addColumn("billid");
        returnedModel.addColumn("itemid");
        returnedModel.addColumn("quantity");
        ///// load item columns
        itemModel = new DefaultTableModel() {

            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        //add column to item table
        tblItems.setModel(itemModel);
        itemModel.addColumn("ItemID");
        itemModel.addColumn("ItemName");
        itemModel.addColumn("Unit");
        itemModel.addColumn("Price");
        itemModel.addColumn("Quantity");
        itemModel.addColumn("returnable");
        itemModel.addColumn("Item Total");

        //load bill into bills table
        this.loadBills();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //method to load item from bills to items table
    private void loadItems() throws SQLException {
        while (itemModel.getRowCount() > 0) {
            //remove old items
            itemModel.removeRow(0);
        }

        //get and set new item into table
        ResultSet rsTemp = objDB.executeSQL("select itemid, quantity from billdetails where billid ='" + tblBills.getValueAt(tblBills.getSelectedRow(), 0).toString() + "'");
        while (rsTemp.next()) {
            rsItem = objDB.executeSQL("select * from items where itemid ='" + rsTemp.getString("itemid") + "'");
            while (rsItem.next()) {
                Vector v = new Vector();
                v.add(rsItem.getString("itemid"));
                v.add((rsItem.getString("itemname")));
                v.add(rsItem.getString("unit"));
                v.add(rsItem.getString("price"));
                v.add(rsTemp.getString("quantity"));
                v.add(rsItem.getString("returnable"));
                v.add(Integer.parseInt(rsTemp.getString("quantity")) * Double.parseDouble(rsItem.getString("price")));
                itemModel.addRow(v);
            }
        }
    }

    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //method to filter table data with keyword and the column passed in
    private void newFilter(String txt, int mode) {
        RowFilter<TableModel, Object> rf = null;
        // If current expression doesn't parse, don't update.
        try {
            rf = RowFilter.regexFilter(txt, mode);
        } catch (java.util.regex.PatternSyntaxException e) {
            return;
        }
        sorter.setRowFilter(rf);
    }
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//method to load bills into bills table

    private void loadBills() throws SQLException {

        //initial bill model table and set it to bill table
        billModel = new DefaultTableModel() {

            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };

        tblBills.setModel(billModel);

        // add column to bill table
        billModel.addColumn("Bill ID");
        billModel.addColumn("Date");
        billModel.addColumn("Sale staff");
        billModel.addColumn("Customer");
        billModel.addColumn("Payment");

        //get data from database and set to bill table
        rsBill = objDB.executeSQL("Select bills.billid, bills.date, bills.customername,bills.payment, staffs.fullname from bills, staffs where bills.username = staffs.username order by date");
        while (rsBill.next()) {
            Vector v = new Vector();
            v.add(rsBill.getString("billid"));
            v.add(rsBill.getString("date"));
            v.add(rsBill.getString("fullname"));
            v.add(rsBill.getString("customerName"));
            v.add(rsBill.getString("payment"));
            billModel.addRow(v);
        }

        //perform filter action when the txtSearch field changed
        sorter = new TableRowSorter<TableModel>(billModel);
        tblBills.setRowSorter(sorter);

        //set option text Search
        txtSearch.getDocument().addDocumentListener(new DocumentListener() {

            @Override
            public void changedUpdate(DocumentEvent e) {
                if (cboSearchBy.getSelectedIndex() == 0) {
                    newFilter(txtSearch.getText(), 0);
                } else if (cboSearchBy.getSelectedIndex() == 1) {
                    newFilter(txtSearch.getText(), 3);
                } else if (cboSearchBy.getSelectedIndex() == 2) {
                    newFilter(txtSearch.getText(), 1);
                }
            }

            @Override
            public void insertUpdate(DocumentEvent e) {
                if (cboSearchBy.getSelectedIndex() == 0) {
                    newFilter(txtSearch.getText(), 0);
                } else if (cboSearchBy.getSelectedIndex() == 1) {
                    newFilter(txtSearch.getText(), 3);
                } else if (cboSearchBy.getSelectedIndex() == 2) {
                    newFilter(txtSearch.getText(), 1);
                }
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                if (cboSearchBy.getSelectedIndex() == 0) {
                    newFilter(txtSearch.getText(), 0);
                } else if (cboSearchBy.getSelectedIndex() == 1) {
                    newFilter(txtSearch.getText(), 3);
                } else if (cboSearchBy.getSelectedIndex() == 2) {
                    newFilter(txtSearch.getText(), 1);
                }
            }
        });

    }

    //method to check whether the receipt has exist
    //return true if existed 
    //return fall if not
    public boolean IsExistReciept(String billid, String itemid) throws SQLException {

        rsReciept = objDB.executeSQL("select billid, itemid from RecieptDetails");
        while (rsReciept.next()) {
            if (rsReciept.getString("billID").equals(billid) && rsReciept.getString("itemid").equals(itemid)) {
                return true;
            }
        }
        return false;
    }

//        String kwDate = txtSearchDate.getText();
//        String kwBillID = txtSearchBillID.getText();
//        String kwCustomerName = txtSearchCustomerName.getText();
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // method to preview bills
    //return true if the previewing successfully
    //false if not
    public boolean preview(String billID) {
        try {
            //use rasper template
            String rp = "reciept.jasper";
            m = new HashMap();
            //pass the parameter to jasper template
            m.put("BillID", billID);
            is = this.getClass().getClassLoader().getResourceAsStream(rp);;
            jp = JasperFillManager.fillReport(is, m, conn);
            JasperViewer jv = new JasperViewer(jp);
            jv.removeWindowListener(jv.getWindowListeners()[0]);
            //set the bill visible
            jv.setVisible(true);
            return true;
        } catch (Exception ex) {
            return false;
        }
    }
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jToolBar7 = new javax.swing.JToolBar();
        jLabel13 = new javax.swing.JLabel();
        btnHome = new javax.swing.JButton();
        jLabel16 = new javax.swing.JLabel();
        jLabel21 = new javax.swing.JLabel();
        jButton9 = new javax.swing.JButton();
        jButton11 = new javax.swing.JButton();
        jLabel19 = new javax.swing.JLabel();
        jButton13 = new javax.swing.JButton();
        jLabel9 = new javax.swing.JLabel();
        jLabel23 = new javax.swing.JLabel();
        jButton12 = new javax.swing.JButton();
        lblBanner = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        lblDate = new javax.swing.JLabel();
        txtSearch = new javax.swing.JTextField();
        cboSearchBy = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        lblItemError = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblItems = new javax.swing.JTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblBills = new javax.swing.JTable();
        lblBillError = new javax.swing.JLabel();

        setBackground(new java.awt.Color(255, 255, 255));
        setMinimumSize(new java.awt.Dimension(700, 300));
        setPreferredSize(new java.awt.Dimension(747, 591));
        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jToolBar7.setBackground(new java.awt.Color(255, 255, 255));
        jToolBar7.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        jToolBar7.setFloatable(false);
        jToolBar7.setRollover(true);
        jToolBar7.setEnabled(false);
        jToolBar7.setName("jToolBar7"); // NOI18N

        jLabel13.setText("         "); // NOI18N
        jLabel13.setToolTipText("");
        jLabel13.setName("jLabel13"); // NOI18N
        jToolBar7.add(jLabel13);

        btnHome.setBackground(new java.awt.Color(255, 255, 255));
        btnHome.setFont(new java.awt.Font("Tahoma", 1, 12));
        btnHome.setForeground(new java.awt.Color(0, 0, 204));
        btnHome.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/home_48.png"))); // NOI18N
        btnHome.setText("Home");
        btnHome.setFocusable(false);
        btnHome.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnHome.setName("btnHome"); // NOI18N
        btnHome.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnHome.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnHomeActionPerformed(evt);
            }
        });
        jToolBar7.add(btnHome);

        jLabel16.setName("jLabel16"); // NOI18N
        jToolBar7.add(jLabel16);

        jLabel21.setText("        ");
        jLabel21.setName("jLabel21"); // NOI18N
        jToolBar7.add(jLabel21);

        jButton9.setBackground(new java.awt.Color(255, 255, 255));
        jButton9.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jButton9.setForeground(new java.awt.Color(0, 0, 204));
        jButton9.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/left_arrow_48.png"))); // NOI18N
        jButton9.setText("  Take back     ");
        jButton9.setFocusable(false);
        jButton9.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton9.setName("jButton9"); // NOI18N
        jButton9.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton9ActionPerformed(evt);
            }
        });
        jToolBar7.add(jButton9);

        jButton11.setBackground(new java.awt.Color(255, 255, 255));
        jButton11.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jButton11.setForeground(new java.awt.Color(0, 0, 204));
        jButton11.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/bill_accept_48.png"))); // NOI18N
        jButton11.setText("   Export reciept ");
        jButton11.setFocusable(false);
        jButton11.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton11.setName("jButton11"); // NOI18N
        jButton11.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton11.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton11ActionPerformed(evt);
            }
        });
        jToolBar7.add(jButton11);

        jLabel19.setText("       ");
        jLabel19.setName("jLabel19"); // NOI18N
        jToolBar7.add(jLabel19);

        jButton13.setBackground(new java.awt.Color(255, 255, 255));
        jButton13.setFont(new java.awt.Font("Tahoma", 1, 12));
        jButton13.setForeground(new java.awt.Color(0, 0, 204));
        jButton13.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/delete_item_48.png"))); // NOI18N
        jButton13.setText("   Undo    ");
        jButton13.setFocusable(false);
        jButton13.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton13.setName("jButton13"); // NOI18N
        jButton13.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton13.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton13ActionPerformed(evt);
            }
        });
        jToolBar7.add(jButton13);

        jLabel9.setText("         ");
        jLabel9.setName("jLabel9"); // NOI18N
        jToolBar7.add(jLabel9);

        jLabel23.setName("jLabel23"); // NOI18N
        jToolBar7.add(jLabel23);

        jButton12.setBackground(new java.awt.Color(255, 255, 255));
        jButton12.setFont(new java.awt.Font("Tahoma", 1, 12));
        jButton12.setForeground(new java.awt.Color(0, 0, 204));
        jButton12.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/help icon 48.png"))); // NOI18N
        jButton12.setText("   Help   ");
        jButton12.setFocusable(false);
        jButton12.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton12.setName("jButton12"); // NOI18N
        jButton12.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton12.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton12ActionPerformed(evt);
            }
        });
        jToolBar7.add(jButton12);

        add(jToolBar7, new org.netbeans.lib.awtextra.AbsoluteConstraints(340, 590, 530, 90));

        lblBanner.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/banner2.jpg"))); // NOI18N
        lblBanner.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        lblBanner.setName("lblBanner"); // NOI18N
        add(lblBanner, new org.netbeans.lib.awtextra.AbsoluteConstraints(-10, 0, 1200, 100));

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Search"));
        jPanel1.setName("jPanel1"); // NOI18N
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        lblDate.setText("Keyword:");
        lblDate.setName("lblDate"); // NOI18N
        jPanel1.add(lblDate, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 20, 50, 20));

        txtSearch.setForeground(new java.awt.Color(204, 204, 204));
        txtSearch.setName("txtSearch"); // NOI18N
        txtSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtSearchActionPerformed(evt);
            }
        });
        txtSearch.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                txtSearchFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtSearchFocusLost(evt);
            }
        });
        jPanel1.add(txtSearch, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 20, 200, -1));

        cboSearchBy.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Bill ID", "Customer Name", "Date" }));
        cboSearchBy.setName("cboSearchBy"); // NOI18N
        jPanel1.add(cboSearchBy, new org.netbeans.lib.awtextra.AbsoluteConstraints(370, 20, 230, -1));

        jLabel1.setText("Search by:");
        jLabel1.setName("jLabel1"); // NOI18N
        jPanel1.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(300, 20, -1, 20));

        add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 120, 610, 60));

        lblItemError.setForeground(new java.awt.Color(255, 51, 0));
        lblItemError.setName("lblItemError"); // NOI18N
        add(lblItemError, new org.netbeans.lib.awtextra.AbsoluteConstraints(180, 400, 800, 20));

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        tblItems.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        tblItems.setName("tblItems"); // NOI18N
        tblItems.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                tblItemsMousePressed(evt);
            }
        });
        jScrollPane1.setViewportView(tblItems);

        add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 430, 1000, 150));

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        tblBills.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        tblBills.setName("tblBills"); // NOI18N
        tblBills.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                tblBillsMousePressed(evt);
            }
        });
        jScrollPane2.setViewportView(tblBills);

        add(jScrollPane2, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 220, 1000, 170));

        lblBillError.setForeground(new java.awt.Color(255, 51, 0));
        lblBillError.setName("lblBillError"); // NOI18N
        add(lblBillError, new org.netbeans.lib.awtextra.AbsoluteConstraints(180, 190, 800, 20));
    }// </editor-fold>//GEN-END:initComponents

    private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton9ActionPerformed
        //set the error notice text to empty
        lblItemError.setText("");
        //check for any items selected or not
        if (tblItems.getSelectedRowCount() > 0) {
            //get item record individually and add the returned item to return table(note that the return table is not visible
            while (tblItems.getSelectedRowCount() > 0) {
                //check if the item can be returned
                if ("1".equals(itemModel.getValueAt(tblItems.getSelectedRow(), 5).toString())) {
                    Vector v = new Vector();
                    //get item informations
                    String billID = (billModel.getValueAt(tblBills.getSelectedRow(), 0).toString());
                    String itemID = (itemModel.getValueAt(tblItems.getSelectedRow(), 0).toString());
                    int quantity = Integer.parseInt(itemModel.getValueAt(tblItems.getSelectedRow(), 4).toString());
                    v.add(billID);
                    v.add(itemID);
                    v.add(quantity);
                    try {
                        //check if the returned item have not been returned before
                        if (!IsExistReciept(billID, itemID)) {
                            //add item to return table
                            returnedModel.addRow(v);
                            //remove this item from items table
                            itemModel.removeRow(tblItems.getSelectedRow());
                        } else {
                            //if the items has been returned 
                            //display error message
                            lblItemError.setText("this item has returned before");
                            //remove item from item table
                            itemModel.removeRow(tblItems.getSelectedRow());
                        }
                    } catch (SQLException ex) {
                        lblItemError.setText("Cannot connect to database");
                    }
                }
            }
        } else {
            try {
                //if no items selected, display this message
                lblItemError.setText("At least one item must be selected");
            } catch (Exception ex) {
                Logger.getLogger(BillManagementPanel.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_jButton9ActionPerformed

    private void jButton11ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton11ActionPerformed
//
        //check for confirmation to save data to database and preview reciept
        if (JOptionPane.showConfirmDialog(null, "Are you sure?") == 0) {
            String billID = "";
            String itemID = "";
            Date d = new Date();
            Format fm = new SimpleDateFormat("MM/dd/YYYY");
            String dateString = fm.format(d);
            //read return table item and insert to reciept table in database
            for (int i = tblReturned.getRowCount(); i >= 0; i--) {
                try {
                    billID = returnedModel.getValueAt(i, 0).toString();
                    itemID = returnedModel.getValueAt(i, 1).toString();
                    int quantity = Integer.parseInt(returnedModel.getValueAt(i, 2).toString());
                    rrh.insertReciept(billID, itemID, quantity, dateString);
                    lblItemError.setText("Insert Successed");
                    //remove this item from return table
                    returnedModel.removeRow(i);

                } catch (Exception ex) {
                    lblItemError.setText("Cannot insert to database, may be the item has been returned before or nothing selected");
                }
            }
            preview(billID);
        }

    }//GEN-LAST:event_jButton11ActionPerformed

    private void tblItemsMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblItemsMousePressed
        // TODO add your handling code here:
    }//GEN-LAST:event_tblItemsMousePressed

    private void txtSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtSearchActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtSearchActionPerformed

    private void txtSearchFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtSearchFocusGained

    }//GEN-LAST:event_txtSearchFocusGained

    private void txtSearchFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtSearchFocusLost
    }//GEN-LAST:event_txtSearchFocusLost

    private void tblBillsMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblBillsMousePressed
        try {
            // TODO add your handling code here:
            //load item in the selected bill into items table
            loadItems();
            //clear return table
            while (tblReturned.getRowCount() > 0) {
                returnedModel.removeRow(0);
            }
        } catch (SQLException ex) {
            Logger.getLogger(BillManagementPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_tblBillsMousePressed

    private void btnHomeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnHomeActionPerformed
       //check null and call the panel
        try {
            InventoryManagement.callJPanel(new SalesPersonPanel(loginUser));
        } catch (SQLException ex) {
            Logger.getLogger(ProduceBillsPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btnHomeActionPerformed

    private void jButton13ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton13ActionPerformed
        //undo return action
        try {
            // TODO add your handling code here:
            //reload item into item table
            loadItems();
            //if the return table empty, display message
            if (tblReturned.getRowCount() <= 0) {
                lblItemError.setText("No action to undo");
            } else {
                //clear return table
                while (tblReturned.getRowCount() > 0) {
                    returnedModel.removeRow(0);
                }
            }
        } catch (SQLException ex) {
            Logger.getLogger(BillManagementPanel.class.getName()).log(Level.SEVERE, null, ex);
        }

    }//GEN-LAST:event_jButton13ActionPerformed

    private void jButton12ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton12ActionPerformed
        // TODO add your handling code here:
        //check null and call the panel
        try {
            // TODO add your handling code here:
            InventoryManagement.OpenManual();
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(null, "cannot locate manual file in 'manual/ISM_Manual.htm'");
        }
    }//GEN-LAST:event_jButton12ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnHome;
    private javax.swing.JComboBox cboSearchBy;
    private javax.swing.JButton jButton11;
    private javax.swing.JButton jButton12;
    private javax.swing.JButton jButton13;
    private javax.swing.JButton jButton9;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel21;
    private javax.swing.JLabel jLabel23;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JToolBar jToolBar7;
    private javax.swing.JLabel lblBanner;
    private javax.swing.JLabel lblBillError;
    private javax.swing.JLabel lblDate;
    private javax.swing.JLabel lblItemError;
    private javax.swing.JTable tblBills;
    private javax.swing.JTable tblItems;
    private javax.swing.JTextField txtSearch;
    // End of variables declaration//GEN-END:variables
}
